import { Component } from '@angular/core'
import { HttpServicesProvider } from '../../../providers/http-services/http-services'
import { ContactPage } from '../../contact/contact'
import {
  IonicPage,
  NavController,
  NavParams,
  ToastController
} from 'ionic-angular'

@IonicPage()
@Component({
  selector: 'page-change-password',
  templateUrl: 'change-password.html'
})
export class ChangePasswordPage {
  info: {
    oldPassword: string
    newPassword: string
    confirmNewPassword: string
  } = {
    oldPassword: '',
    newPassword: '',
    confirmNewPassword: ''
  }
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private toastCtrl: ToastController,
    private httpService: HttpServicesProvider
  ) {}

  changePassword() {
    if (!this.checkNewPw()) {
      this.presentToast('两次输入的新密码不一致')
      return
    }
    if (
      this.info.newPassword &&
      this.info.confirmNewPassword &&
      this.info.oldPassword
    ) {
      let data = {}
      data['password'] = this.info.oldPassword
      data['newPassword'] = this.info.newPassword
      // data['confirmNewPassword'] = this.info.confirmNewPassword
      this.httpService.requestDataPost(
        'sys/user/password',
        data,
        {},
        (status, datas) => {
          if (status) {
            let params = datas.message
            this.presentToastsave(params)
          }
        }
      )
    } else {
      this.presentToast('选项不能为空')
      return
    }
  }
  checkNewPw() {
    if (this.info.newPassword && this.info.confirmNewPassword) {
      if (this.info.newPassword === this.info.confirmNewPassword) {
        return true
      } else {
        return false
      }
    }
  }
  presentToast(message) {
    let toast = this.toastCtrl.create({
      message: message,
      duration: 2000,
      position: 'middle',
      dismissOnPageChange: true
    })

    toast.onDidDismiss(() => {
      console.log('Dismissed toast')
    })

    toast.present()
  }
  ionViewDidLoad() {
    console.log('ionViewDidLoad ChangePasswordPage')
  }
  presentToastsave(message) {
    let toast = this.toastCtrl.create({
      message: message,
      duration: 1500,
      position: 'middle'
    })

    toast.onDidDismiss(() => {
      this.navCtrl.pop()
    })
    toast.present()
  }
}
